import {Request, Response} from "express";
import Constant from "@src/util/constant";
import regionSql from "@src/sql/regionSql";
import RegionSql from "@src/sql/regionSql";
import literatureSql from "@src/sql/literatureSql";
import articleSql from "@src/sql/articleSql";
import {getMeta} from "@src/util/util";

const update = async (req: Request) => {
  const {content, images, videos} = req.body;
  const {id} = req.params;
  // 参数类型验证
  if (!(images instanceof Array && videos instanceof Array)) {
    return Constant.PARAMETER_NOT_ARR;
  }
  // 是否存在该区域
  if (!(await regionSql.isRegionId(id))) {
    return Constant.REGION_NOT_ERROR;
  }
  return await RegionSql.update(content, images, videos, id);
};

// 关联文献
const relationLiterature = async (id: string, literature_id: string) => {
  // 是否存在该区域
  if (!(await regionSql.isRegionDictId(id))) {
    return Constant.REGION_NOT_ERROR;
  }
  // 是否存在该文献
  if (!(await literatureSql.isLiterature(literature_id))) {
    return Constant.LITERATURE_NOT_ERROR;
  }
  return await RegionSql.relationLiterature(id, literature_id);
};

// 关联文章
const relationArticle = async (id: string, article_id: string) => {
  // 是否存在该区域
  if (!(await regionSql.isRegionDictId(id))) {
    return Constant.REGION_NOT_ERROR;
  }
  // 是否存在该文章
  if (!(await articleSql.isArticleId(article_id))) {
    return Constant.ARTICLE_NOT_ERROR;
  }
  return await RegionSql.relationArticle(id, article_id);
};

// 取消文献关联
const cancelRelationLiterature = async (id: string) => {
  return await regionSql.cancelRelationLiterature(id);
};

// 取消文章关联
const cancelRelationArticle = async (id: string) => {
  // 是否存在该文章
  if (!(await articleSql.isArticleId(id))) {
    return Constant.ARTICLE_NOT_ERROR;
  }
  return await regionSql.cancelRelationArticle(id);
};

// 区域详情
const detail = async (id: string) => {
  // 是否存在该区域
  if (!(await regionSql.isRegionDictId(id))) {
    return Constant.REGION_NOT_ERROR;
  }
  return await regionSql.detail(id);
};

// 文章关联列表
const relationArticleList = async (page: number, size: number, filters: any, id: string) => {
  // 是否存在该区域
  if (!(await regionSql.isRegionDictId(id))) {
    return Constant.REGION_NOT_ERROR;
  }
  const offset = (page - 1) * size;
  const {
    count,
    rows,
  } = await regionSql.relationArticleList(offset, size, filters, id);

  const meta = getMeta(count, page, size);
  return {
    ...Constant.DEFAULT_SUCCESS,
    meta,
    data: rows,
  };
};

// 文献关联列表
const relationLiteratureList = async (page: number, size: number, filters: any, id: string) => {
  // 是否存在该区域
  if (!(await regionSql.isRegionDictId(id))) {
    return Constant.REGION_NOT_ERROR;
  }
  const offset = (page - 1) * size;
  const {
    count,
    rows,
  } = await regionSql.relationLiteratureList(offset, size, filters, id);
  const meta = getMeta(count, page, size);
  return {
    ...Constant.DEFAULT_SUCCESS,
    meta,
    data: rows,
  };
};

export default {
  update,
  relationLiterature,
  relationArticle,
  cancelRelationLiterature,
  cancelRelationArticle,
  detail,
  relationLiteratureList,
  relationArticleList,
};